package com.cc.advice;


import com.cc.util.ConditionException;
import com.cc.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

import static com.cc.constant.BusinessStatusConstant.BUSINESS_EXCEPTION;
import static com.cc.constant.BusinessStatusConstant.BUSINESS_EXCEPTION_MESSAGE;


/**
 * @author CHENCHEN
 * @company 北京学佳澳教育科技
 * @create 2023-03-02 15:49
 */
@Slf4j
@RestControllerAdvice
public class GloableExceptionAdvice {

    @ExceptionHandler(value = Exception.class)
    public CommonResponse handlerFlyingFishException(HttpServletRequest request, Exception e){
        log.error("FlyingFish exception : [{}]",e.getMessage(), e);
        if(e instanceof ConditionException){
            Integer code = ((ConditionException) e).getCode();
            return CommonResponse.BUSINESS_FAIL(code, e.getMessage());
        }
        return CommonResponse.BUSINESS_FAIL(BUSINESS_EXCEPTION, BUSINESS_EXCEPTION_MESSAGE, e);
    }
}
